idf_component_register(SRCS "network_interfaces/uros_ethernet_netif.c" "network_interfaces/uros_wlan_netif.c"
                       INCLUDE_DIRS "network_interfaces"
                       REQUIRES nvs_flash)

if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
set(submake "$(MAKE)")
else()
set(submake "make")
endif()

set(APP_COLCON_META "${PROJECT_DIR}/app-colcon.meta")
if(NOT EXISTS "${APP_COLCON_META}")
set(APP_COLCON_META "")
endif()

set(EXTRA_ROS_PACKAGES "${PROJECT_DIR}/extra_ros_packages")
if(NOT EXISTS "${EXTRA_ROS_PACKAGES}")
set(EXTRA_ROS_PACKAGES "${COMPONENT_DIR}/extra_packages")
endif()

externalproject_add(libmicroros_project
    PREFIX     ${CMAKE_BINARY_DIR}/libmicroros-prefix
    SOURCE_DIR ${COMPONENT_DIR}
    BINARY_DIR ${COMPONENT_DIR}
    CONFIGURE_COMMAND ""
    BUILD_COMMAND
        ${submake} -j -f libmicroros.mk
            X_CC=${CMAKE_C_COMPILER}
            X_AR=${CMAKE_AR}
            X_STRIP=${CMAKE_STRIP}
            X_CFLAGS=${CMAKE_C_FLAGS}
            X_CXX=${CMAKE_CXX_COMPILER}
            X_CXXFLAGS=${CMAKE_CXX_FLAGS}
            BUILD_DIR=${CMAKE_BINARY_DIR}
            IDF_PATH=${IDF_PATH}
            IDF_TARGET=${IDF_TARGET}
            APP_COLCON_META=${APP_COLCON_META}
            IDF_VERSION_MAJOR=${IDF_VERSION_MAJOR}
            IDF_VERSION_MINOR=${IDF_VERSION_MINOR}
            EXTRA_ROS_PACKAGES=${EXTRA_ROS_PACKAGES}
    INSTALL_COMMAND ""
    BUILD_BYPRODUCTS ${COMPONENT_DIR}/libmicroros.a
    )

add_prebuilt_library(libmicroros-prebuilt ""
                     REQUIRES lwip)

add_dependencies(libmicroros-prebuilt libmicroros_project)

set_target_properties(libmicroros-prebuilt PROPERTIES IMPORTED_LOCATION ${COMPONENT_DIR}/libmicroros.a)
target_include_directories(libmicroros-prebuilt INTERFACE ${COMPONENT_DIR}/include)

add_dependencies(${COMPONENT_LIB} libmicroros-prebuilt)
target_link_libraries(${COMPONENT_LIB} INTERFACE libmicroros-prebuilt)

set_directory_properties( PROPERTIES ADDITIONAL_CLEAN_FILES
    "${COMPONENT_DIR}/include;${COMPONENT_DIR}/micro_ros_dev;${COMPONENT_DIR}/micro_ros_src;${COMPONENT_DIR}/esp32_toolchain.cmake" )

